﻿using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Resources;
using System.IO;

using OpenTK;
using OpenTK.Graphics.OpenGL;

namespace GLSL
{
    public class VBO
    {
        private int VertexBufferObjectID;
        private Vertex[] vertexs;

        public VBO()
        {
            GL.GenBuffers(1, out this.VertexBufferObjectID);
        }

        public void SetData(Vertex[] vertex)
        {
            GL.BindBuffer(BufferTarget.ArrayBuffer, this.VertexBufferObjectID);
            GL.BufferData<Vertex>(BufferTarget.ArrayBuffer, (IntPtr)(vertex.Length * Vertex.Stride), vertex, BufferUsageHint.StaticDraw);
            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
            this.vertexs = vertex;
        }

        public void Render()
        {
            GL.BindBuffer(BufferTarget.ArrayBuffer, this.VertexBufferObjectID);

            GL.EnableClientState(ArrayCap.VertexArray);
            GL.EnableClientState(ArrayCap.TextureCoordArray);
            GL.EnableClientState(ArrayCap.NormalArray);

            GL.VertexPointer(3, VertexPointerType.Float, Vertex.Stride, new IntPtr(0));
            GL.TexCoordPointer(2, TexCoordPointerType.Float, Vertex.Stride, new IntPtr(12));
            GL.NormalPointer(NormalPointerType.Float, Vertex.Stride, new IntPtr(20));

            //Draw
            GL.DrawArrays(BeginMode.Quads, 0, vertexs.Length);

            GL.DisableClientState(ArrayCap.VertexArray);
            GL.DisableClientState(ArrayCap.TextureCoordArray);
            GL.EnableClientState(ArrayCap.NormalArray);

            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
        }
    }
}
